﻿using System.Collections.Generic;
using FairyGUI;
using System;
using UnityEngine;
using System.IO;

public class FCommonListMediator : MyWindow
{

    GList _list;
	public GList list{
		get {return _list;}
		private set{_list = value;}
	}
	public string title{
		set{
			this.contentPane.GetChild("frame").asLabel.title = value;
		}
	}
    public Action<int> loadData;
    protected override void OnInit()
    {
        base.OnInit();
    }

	public FCommonListMediator(){
		this.contentPane = UIPackage.CreateObject("MapPackages", "CommonReadList").asCom;
        _list = this.contentPane.GetChild("List").asList;

        this.contentPane.GetChild("loadBtn").asButton.onClick.Add(() =>
        {
            if (_list.selectedIndex == -1)
            {
                FUIMain.TipPanel("请选择要读取的选项！");
                return;
            }

            if (loadData != null) {
                loadData(_list.selectedIndex);
                FUIMain.ClosePanel(GMConfig.PanelType.CommonListPanel);
            }
        });

        this.contentPane.GetChild("closeBtn").onClick.Add(() =>
        {
            FUIMain.ClosePanel(GMConfig.PanelType.CommonListPanel);
        });
    }
    public void SetLoadWithProduct(GMConfig.ProductType productType, 
            Dictionary<GMConfig.ProductType, GList> _lists, Map _map, 
            List<FixedRoom> fixedRoomList, FileInfo[] blocks){
        GList list;
        loadData = (int index) =>
        {
            switch (productType)
            {
                case GMConfig.ProductType.Card:
                    List<Card> _cardDataList = Utils.LoadByJson<List<Card>>(Application.dataPath, "cardList.json");
                    Card data = _cardDataList[index];

                    MapManager.Instance.AddKeyValue(_map, GMConfig.ProductType.Card, new KeyValue(data.name, 0));
                    list = _lists[GMConfig.ProductType.Card];
                    list.numItems++;
                    break;
                case GMConfig.ProductType.InsertRoom:
                    FixedRoom room = fixedRoomList[index];
                    MapManager.Instance.AddKeyValue(_map, GMConfig.ProductType.InsertRoom, new KeyValue(room.name, 0));
                    list = _lists[GMConfig.ProductType.InsertRoom];
                    list.numItems++;
                    break;
                case GMConfig.ProductType.Event:
                    FixedRoom froom = fixedRoomList[index];
                    MapManager.Instance.AddKeyValue(_map, GMConfig.ProductType.Event, new KeyValue(froom.name, 0));
                    list = _lists[GMConfig.ProductType.Event];
                    list.numItems++;
                    break;
                case GMConfig.ProductType.Boss:
                    List<Biology> bossList = BiologyMO.GetListOfType(GMConfig.BiologyType.Boss);
                    Biology biology = bossList[index];
                    MapManager.Instance.AddKeyValue(_map, GMConfig.ProductType.Boss, new KeyValue(biology.name, 0));
                    list = _lists[GMConfig.ProductType.Boss];
                    list.numItems++;
                    break;
                case GMConfig.ProductType.BossRetinue:
                    List<Biology> bossRetinueList = BiologyMO.GetListOfType(GMConfig.BiologyType.Normal);
                    Biology bossRetinue = bossRetinueList[index];
                    MapManager.Instance.AddKeyValue(_map, GMConfig.ProductType.BossRetinue, new KeyValue(bossRetinue.name, 0));
                    list = _lists[GMConfig.ProductType.BossRetinue];
                    list.numItems++;
                    break;
                case GMConfig.ProductType.Elite:
                    List<Biology> eliteList = BiologyMO.GetListOfType(GMConfig.BiologyType.Elites);
                    Biology elite = eliteList[index];
                    MapManager.Instance.AddKeyValue(_map, GMConfig.ProductType.Elite, new KeyValue(elite.name, 0));
                    list = _lists[GMConfig.ProductType.Elite];
                    list.numItems++;
                    break;
                case GMConfig.ProductType.EliteRetinue:
                    List<Biology> eliteRetinueList = BiologyMO.GetListOfType(GMConfig.BiologyType.Normal);
                    Biology eliteRetinue = eliteRetinueList[index];
                    MapManager.Instance.AddKeyValue(_map, GMConfig.ProductType.EliteRetinue, new KeyValue(eliteRetinue.name, 0));
                    list = _lists[GMConfig.ProductType.EliteRetinue];
                    list.numItems++;
                    break;
                case GMConfig.ProductType.Monster:
                    List<Biology> monsterList = BiologyMO.GetListOfType(GMConfig.BiologyType.Normal);
                    Biology monster = monsterList[index];
                    MapManager.Instance.AddKeyValue(_map, GMConfig.ProductType.Monster, new KeyValue(monster.name, 0));
                    list = _lists[GMConfig.ProductType.Monster];
                    list.numItems++;
                    break;
                case GMConfig.ProductType.BossRoom:
                case GMConfig.ProductType.EliteRoom:
                case GMConfig.ProductType.MonsterRoom:
                    if (blocks == null)
                        blocks = Utils.GetFiles(Application.dataPath + "/Resources/Map/Blocks", "*.prefab");
                    string name = Path.GetFileNameWithoutExtension(blocks[index].ToString());
                    MapManager.Instance.AddKeyValue(_map, productType, new KeyValue(name, 0, 1));
                    list = _lists[productType];
                    list.numItems++;
                    break;
            }
        };
    }

    public void SetRenderer(GMConfig.ProductType productType,
            List<FixedRoom> fixedRoomList,
            FileInfo[] blocks){
        _list.itemRenderer = (int index, GObject obj) =>
               {
                   GButton item = obj.asButton;
                   switch (productType)
                   {
                       case GMConfig.ProductType.Card:
                           List<Card> _cardDataList = Utils.LoadByJson<List<Card>>(Application.dataPath, "cardList.json");
                           Card card = _cardDataList[index];
                           item.asCom.GetChild("IdTextField").asTextField.text = index.ToString();
                           item.asCom.GetChild("nameTextField").asTextField.text = card.name;
                           item.asCom.GetChild("rectTextField").asTextField.text = "";
                           break;
                       case GMConfig.ProductType.InsertRoom:
                           FixedRoom room = fixedRoomList[index];
                           item.asCom.GetChild("IdTextField").asTextField.text = index.ToString();
                           item.asCom.GetChild("nameTextField").asTextField.text = room.name;
                           item.asCom.GetChild("rectTextField").asTextField.text = string.Format("{0} * {1}", room.width, room.height);
                           break;
                       case GMConfig.ProductType.Event:
                           FixedRoom froom = fixedRoomList[index];
                           item.asCom.GetChild("IdTextField").asTextField.text = index.ToString();
                           item.asCom.GetChild("nameTextField").asTextField.text = froom.name;
                           item.asCom.GetChild("rectTextField").asTextField.text = string.Format("{0} * {1}", froom.width, froom.height);
                           break;
                       case GMConfig.ProductType.Boss:
                           List<Biology> biologyList = BiologyMO.GetListOfType(GMConfig.BiologyType.Boss);
                           Biology biology = biologyList[index];
                           item.asCom.GetChild("IdTextField").asTextField.text = index.ToString();
                           item.asCom.GetChild("nameTextField").asTextField.text = biology.name;
                           item.asCom.GetChild("rectTextField").asTextField.text = biology.descript;
                           break;
                       case GMConfig.ProductType.BossRetinue:
                           List<Biology> bossRetinueList = BiologyMO.GetListOfType(GMConfig.BiologyType.Normal);
                           Biology bossRetinue = bossRetinueList[index];
                           item.asCom.GetChild("IdTextField").asTextField.text = index.ToString();
                           item.asCom.GetChild("nameTextField").asTextField.text = bossRetinue.name;
                           item.asCom.GetChild("rectTextField").asTextField.text = bossRetinue.descript;
                           break;
                       case GMConfig.ProductType.Elite:
                           List<Biology> eliteList = BiologyMO.GetListOfType(GMConfig.BiologyType.Elites);
                           Biology elite = eliteList[index];
                           item.asCom.GetChild("IdTextField").asTextField.text = index.ToString();
                           item.asCom.GetChild("nameTextField").asTextField.text = elite.name;
                           item.asCom.GetChild("rectTextField").asTextField.text = elite.descript;
                           break;
                       case GMConfig.ProductType.EliteRetinue:
                           List<Biology> eliteRetinueList = BiologyMO.GetListOfType(GMConfig.BiologyType.Normal);
                           Biology eliteRetinue = eliteRetinueList[index];
                           item.asCom.GetChild("IdTextField").asTextField.text = index.ToString();
                           item.asCom.GetChild("nameTextField").asTextField.text = eliteRetinue.name;
                           item.asCom.GetChild("rectTextField").asTextField.text = eliteRetinue.descript;
                           break;
                       case GMConfig.ProductType.Monster:
                           List<Biology> monsterList = BiologyMO.GetListOfType(GMConfig.BiologyType.Normal);
                           Biology monster = monsterList[index];
                           item.asCom.GetChild("IdTextField").asTextField.text = index.ToString();
                           item.asCom.GetChild("nameTextField").asTextField.text = monster.name;
                           item.asCom.GetChild("rectTextField").asTextField.text = monster.descript;
                           break;
                       case GMConfig.ProductType.BossRoom:
                       case GMConfig.ProductType.EliteRoom:
                       case GMConfig.ProductType.MonsterRoom:
                           if (blocks == null)
                               blocks = Utils.GetFiles(Application.dataPath + "/Resources/Map/Blocks", "*.prefab");
                           string name = Path.GetFileNameWithoutExtension(blocks[index].ToString());
                           item.asCom.GetChild("IdTextField").asTextField.text = index.ToString();
                           item.asCom.GetChild("nameTextField").asTextField.text = name;
                           break;
                   }
               };
        _list.numItems = MapManager.Instance.GetDataCount(productType);
        title = MapManager.Instance.GetTitle(productType);
    }
    protected override void OnShown(){
	}
}
